//
//  VKIphoneController.m
//  V-Learn
//
//  Created by liubo on 2019/1/15.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "VKIphoneController.h"
#import "SNSCodeCountdownButton.h"
#import "LBLoginVC.h"
@interface VKIphoneController ()<SNSCodeCountdownButtonDelegate,UITextFieldDelegate>
@property (nonatomic, strong) UITextField *editTf;
@property (nonatomic, strong) UITextField *qrTf;
@property (nonatomic, strong) UITextField *passwordTf;

@property (nonatomic, strong) SNSCodeCountdownButton *yanZhengButton;

@end

@implementation VKIphoneController

-(UITextField *)editTf{
    if (_editTf == nil) {
        _editTf = [[UITextField alloc]init];
    }
    return _editTf;
}

-(UITextField *)qrTf{
    if (_qrTf == nil) {
        _qrTf = [[UITextField alloc]init];
    }
    return _qrTf;
}
-(UITextField *)passwordTf{
    if (_passwordTf == nil) {
        _passwordTf = [[UITextField alloc]init];
        _passwordTf.secureTextEntry = YES;
        _passwordTf.delegate = self;
    }
    return _passwordTf;
}
-(UIButton *)yanZhengButton{
    if (_yanZhengButton == nil) {
        _yanZhengButton = [SNSCodeCountdownButton buttonWithType:UIButtonTypeCustom];
        _yanZhengButton.hex = 0x9D9D9D;
        _yanZhengButton.titleLabel.font = [UIFont systemFontOfSize:13];
        _yanZhengButton.countdownBeginNumber = 60;
        _yanZhengButton.delegate = self;
    }
    return _yanZhengButton;
}
- (instancetype)initWithRow:(NSInteger)row;
{
    self = [super init];
    if (self) {
        self.row = row;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor cz_colorWithRed:244 green:244 blue:244];
    self.edgesForExtendedLayout = @[];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.view addSubview:self.passwordTf];
    self.passwordTf.backgroundColor = [UIColor whiteColor];
    UIView *passView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 15, 10)];
    self.passwordTf.clearButtonMode = UITextFieldViewModeAlways;
    self.passwordTf.leftViewMode = UITextFieldViewModeAlways;
    self.passwordTf.leftView = passView;
    self.passwordTf.placeholder = @"请输入密码";
    [self.passwordTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.height.equalTo(@53);
        make.top.mas_equalTo(@15);
    }];
    UIView *passwordView = [[UIView alloc]init];
    [self.view addSubview:passwordView];
    passwordView.backgroundColor = [UIColor cz_colorWithRed:244 green:244 blue:244];
    [passwordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@1);
        make.left.right.equalTo(@0);
        make.top.mas_equalTo(self.passwordTf.mas_bottom);
    }];
    
    [self.view addSubview:self.editTf];
    self.editTf.backgroundColor = [UIColor whiteColor];
    UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 15, 10)];
    self.editTf.clearButtonMode = UITextFieldViewModeAlways;
    self.editTf.leftViewMode = UITextFieldViewModeAlways;
    self.editTf.leftView = leftView;
    [self.editTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.height.equalTo(@53);
        make.top.mas_equalTo(passwordView.mas_bottom);
    }];
    
    UIView *lineView = [[UIView alloc]init];
    [self.view addSubview:lineView];
    lineView.backgroundColor = [UIColor cz_colorWithRed:244 green:244 blue:244];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@1);
        make.left.right.equalTo(@0);
        make.top.mas_equalTo(self.editTf.mas_bottom);
    }];

    
    [self.view addSubview:self.qrTf];
    self.qrTf.backgroundColor = [UIColor whiteColor];
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 15, 10)];
    self.qrTf.clearButtonMode = UITextFieldViewModeAlways;
    self.qrTf.leftViewMode = UITextFieldViewModeAlways;
    self.qrTf.leftView = view;
    self.qrTf.placeholder = @"请输入验证码";
    [self.qrTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.mas_equalTo(self.view).offset(-100);
        make.height.equalTo(@53);
        make.top.mas_equalTo(lineView.mas_bottom);
    }];
    
    
    [self.view addSubview:self.yanZhengButton];
    _yanZhengButton.backgroundColor = [UIColor whiteColor];
    [_yanZhengButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view);
        make.width.equalTo(@100);
        make.centerY.equalTo(self.qrTf);
        make.top.mas_equalTo(lineView.mas_bottom);
    }];
    
    UIView *xilineView = [[UIView alloc]init];
    [self.view addSubview:xilineView];
    xilineView.backgroundColor = [UIColor cz_colorWithRed:220 green:220 blue:220];
    [xilineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@1);
        make.centerY.equalTo(self.qrTf);
        make.left.mas_equalTo(self.yanZhengButton);
        make.height.equalTo(@23);
    }];
    if (self.row == 0) {
        self.editTf.placeholder = @"请输入昵称";
    }else if (self.row == 3){
        self.editTf.placeholder = @"请输入手机号";
    }
    
    UIButton *commitBtn = [[UIButton alloc]init];
    [self.view addSubview:commitBtn];
    [commitBtn setImage:[UIImage imageNamed:@"vkbtn"] forState:UIControlStateNormal];
    [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-135);
        make.left.equalTo(@15);
        make.width.equalTo(@([UIScreen cz_screenWidth] - 30));
        make.height.equalTo(@55);
    }];
    commitBtn.imageView.contentMode = UIViewContentModeScaleToFill;
    [commitBtn addTarget:self action:@selector(commitBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *commitLabel = [[UILabel alloc]init];
    [self.view addSubview:commitLabel];
    commitLabel.textColor = [UIColor whiteColor];
    commitLabel.font = [UIFont systemFontOfSize:18];
    commitLabel.text = @"确定";
    [commitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(commitBtn).offset(15);
        make.centerX.equalTo(commitBtn);
    }];
}
/**
 代理获取验证码
 */
- (void)snsCodeCountdownButtonClicked {
    NSString *content = [self.editTf.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ((content.length == 0) || [content isEqualToString:@""]) {
        [MBProgressHUD showSuccess:@"请输入手机号" toView:self.view];
        return;
    }
    
    if (![self checkTelNumber:content]) {
        [MBProgressHUD showSuccess:@"请输入正确的手机号" toView:self.view];
        return;
    }
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"正在加载...";
    [hud show:YES];
    VKWeakSelf(self)
    [WYHXNetWorkService netWorkSerViceRequestWithGetUrl:WKMeGetCode withPostParams:@{@"tel":[UserModel getUserModel].tel}.mutableCopy withBlock:^(NSDictionary *result, NSError *error) {
        hud.hidden = YES;
        if ([result[@"code"]integerValue] == 200) {
            [MBProgressHUD showSuccess:@"获取成功" toView:self.view];
            [weakself.yanZhengButton initWithCountdownBeginNumber];
        }else{
            [MBProgressHUD showSuccess:result[@"msg"] toView:self.view];
        }
    }];
    
}
- (void)commitBtnClick{
    
    NSString *password = [self.passwordTf.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ((password.length == 0) || [password isEqualToString:@""]) {
        [MBProgressHUD showSuccess:@"请输入密码" toView:self.view];
        return;
    }
    
    if (![self checkPassword:_passwordTf.text]) {
        [MBProgressHUD showSuccess:@"请输入6至12位数字和字母组合的密码" toView:self.view];
        return;
    }
    
    NSString *content = [self.editTf.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ((content.length == 0) || [content isEqualToString:@""]) {
        [MBProgressHUD showSuccess:@"请输入手机号" toView:self.view];
        return;
    }
    
    if (![self checkTelNumber:content]) {
        [MBProgressHUD showSuccess:@"请输入正确的手机号" toView:self.view];
        return;
    }
    
    NSString *qrcontent = [self.qrTf.text stringByReplacingOccurrencesOfString:@" " withString:@""];

    if ((qrcontent.length == 0) || [qrcontent isEqualToString:@""]) {
        [MBProgressHUD showSuccess:@"请输入验证码" toView:self.view];
        return;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"正在加载...";
    [hud show:YES];

    [WYHXNetWorkService netWorkSerViceRequestWithGetUrl:WKMeGetCode withPostParams:@{@"token":[UserModel getUserModel].token,@"tel":content,@"code":qrcontent,@"password":password}.mutableCopy withBlock:^(NSDictionary *result, NSError *error) {
        hud.hidden = YES;
        if ([result[@"code"]integerValue] == 200) {
            [MBProgressHUD showSuccess:@"修改成功" toView:self.view];
            [self performSelector:@selector(updateClick) withObject:nil afterDelay:0.7];
        }else{
            [MBProgressHUD showSuccess:result[@"msg"] toView:self.view];
        }
    }];
    
//    if (_infoBlock) {
//        self.infoBlock(content);
//        [self.navigationController popViewControllerAnimated:YES];
//    }
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    if (textField == _passwordTf) {
        if (![self checkPassword:_passwordTf.text]) {
            [MBProgressHUD showSuccess:@"请输入6至12位数字和字母组合的密码" toView:self.view];
        }
    }
    return YES;
}
- (BOOL)checkPassword:(NSString *) password
{
    NSString *pattern = @"^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,12}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:password];
    return isMatch;
    
}
- (void)updateClick{
    [UIApplication sharedApplication].keyWindow.rootViewController = [[LBLoginVC alloc]init];
}
- (BOOL)checkTelNumber:(NSString *)elNumber{
    NSString *pattern =@"^1+[35789]+\\d{9}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
    BOOL isMatch = [pred evaluateWithObject:elNumber];
    
    return isMatch;
}
@end
